﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;

namespace Utils
{
    public class StringUtils
    {
        /// <summary>
        /// 单词首字母大写
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToTitleCase(string str)
        {
            CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
            TextInfo textInfo = cultureInfo.TextInfo;
            return textInfo.ToTitleCase(str.ToLower());
        }
        /// <summary>
        /// 单词首字母小写
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToTitleLower(string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                string tmpStr = str.Substring(0, 1);
                tmpStr = tmpStr.ToLower();
                return tmpStr + str.Substring(1);
            }
            return "";
        }
        /// <summary>
        /// 获取给定值的另名，返回结果为替换特殊字符后的各部分首字母大写
        /// 如：sys_menu  @return SysMenu
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetAlias(string name)
        {
            var tableNameArr = name.Split('_');
            string nameAlias = "";
            for (int j = 0; j < tableNameArr.Length; j++)
            {
                nameAlias += ToTitleCase(tableNameArr[j]);
            }
            return nameAlias;
        }
        /// <summary>
        /// 编码
        /// </summary>
        /// <param name="str"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static  string Encode(string str,Encoding encoding)
        {
            return HttpUtility.UrlEncode(str, encoding);
        }
    }
}
